package com.ebelter.seneo.ui.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.ebelter.btcomlib.utils.DipUtil;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;

import java.util.Objects;

/**
 * 指令选择器
 */

public class NumSelectorView extends View {


    String TAG = "NumSelectorView";

    public NumSelectorView(Context context) {
        this(context,null);
    }

    public NumSelectorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    int mWidth;//整个控件的宽度
    int mHeight;//整个控件的高度

    int topKeduHeight;
    int buttomKeduHeight;
    Paint mPaint;
    public int dp1;


    private float currentDisplayValue;
    private float minNum = 10;
    private float maxNum = 120;

    //X轴的偏移量
    private float xInit;
    //最大偏移量
    private float maxXInit;
    //最小偏移量
    private float minXInit;

    public void setMinMaxNum(float minNum,float maxNum) {
        this.minNum = minNum;
        this.maxNum = maxNum;
        maxXInit=(maxNum-minNum)*dp1*8;
    }



    private void init(Context context) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        mPaint.setStrokeWidth(dp1);
        dp1 = DipUtil.dip2px(context,1);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        initBaseData();
    }



    private void initBaseData() {
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
        out("---initBaseData--mWidth = "+mWidth+"--mHeight = "+mHeight);

        topKeduHeight = mHeight/3;
        buttomKeduHeight=mHeight*2/3;
        //三角形的path
        arrowPath = new Path();
        arrowPath.moveTo(mWidth/2,0);
        arrowPath.lineTo(mWidth/2+dp1*9,dp1*21);
        arrowPath.lineTo(mWidth/2-dp1*9,dp1*21);
        arrowPath.close();

        maxXInit=(maxNum-minNum)*dp1*8;
    }



    public void setValue(float value){
        float v2=(value-minNum)*(dp1*8);
        xInit=v2;
        invalidate();


    }

    @Override
    protected void onDraw(Canvas canvas) {

        drawKedu(canvas);//画刻度

        drawMiddleArrow(canvas);//画中间箭头

    }

    private void drawKedu(Canvas canvas) {



        mPaint.setTextSize(DipUtil.sp2px(getContext(),18));
        int j=0;
        for (float i = minNum;i<=maxNum;i++){


            Object[] objects=isChang(i);
            boolean isChang = (boolean) objects[0];
            float startX = mWidth/2+j*dp1*8-xInit;
            float stopY;
            if (isChang){
                mPaint.setStrokeWidth((float) (dp1*2.5));
                stopY=buttomKeduHeight;

                String text = (String) objects[1];
                if (!TextUtils.isEmpty(text)){
                    float textX=startX-DipUtil.getTextWidth(mPaint,text)/2;
                    float textY=buttomKeduHeight+DipUtil.getTextHeight(mPaint,text)+dp1*8;
                    mPaint.setColor(textColor);
                    canvas.drawText(text,textX,textY,mPaint);
                }

            }else {
                mPaint.setStrokeWidth((float) (dp1*1.5));
                stopY=buttomKeduHeight-dp1*8;
            }
            mPaint.setColor(keduColor);
            canvas.drawLine(startX,topKeduHeight,startX,stopY,mPaint);
            j++;

        }

        out("---j="+j);

    }

    private Object[] isChang(float value){
        Object[] objects = new Object[2];
        //float temp1 = NumUtils.numBaoLiuWei1(value);
       // int temp2 = (int) (temp1*10);
        int temp3 = (int) value;
        if (temp3%5==0){
            objects[0]=true;
            if (temp3%10==0){
                objects[1]=temp3+"";
            }else {
                objects[1]=null;
            }
        }else {
            objects[0]=false;
            objects[1]=null;

        }


        return objects;


    }



    private int arrowColor = Color.parseColor("#FF4BB0FE");
    private int keduColor = Color.parseColor("#FF8B8C8E");
    private int textColor = Color.parseColor("#FF030303");
    private Path arrowPath;
    //还中间箭头
    private void drawMiddleArrow(Canvas canvas) {

        mPaint.setStyle(Paint.Style.FILL);//实心
        mPaint.setColor(arrowColor);
        mPaint.setStrokeWidth((float) (dp1*3.5));
        canvas.drawPath(arrowPath,mPaint);
        canvas.drawLine(mWidth/2,dp1*21,mWidth/2,buttomKeduHeight,mPaint);
    }

    private void out(String msg){
//        if (false){
//            Log.i(TAG,msg);
//        }
    }


    private ValueChangeListener mValueChangeListener;

    public void setmValueChangeListener(ValueChangeListener mValueChangeListener) {
        this.mValueChangeListener = mValueChangeListener;
    }

    public float getValue() {
        float oneUnit = xInit/(dp1*8);
        return minNum+oneUnit;
    }

    public interface ValueChangeListener{
        void changeValue(float value);
    }


    private void changeValue() {

        if (mValueChangeListener!=null){
            float oneUnit = xInit/(dp1*8);
            mValueChangeListener.changeValue(minNum+oneUnit);
        }
    }



    int mPosX;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //super.onTouchEvent(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPosX = (int) event.getX();
                out("---onTouchEvent");
                break;
            case MotionEvent.ACTION_MOVE:


                float dis = mPosX-event.getX();
                mPosX = (int) event.getX();

                out("---ACTION_MOVE 前 --- xInit = "+xInit+"---disdis = "+dis+"---maxXInit="+maxXInit+"---minXInit"+minXInit);

                if (xInit + dis< minXInit) {
                    xInit = minXInit;
                  //  LogUtils.i(TAG,"------A----xInit = minXInit ="+minXInit);
                } else if (xInit + dis > maxXInit) {
                    xInit = maxXInit;
                   // LogUtils.i(TAG,"------B----xInit = maxXInit ="+maxXInit);
                } else {
                    xInit = xInit + dis;
                    //LogUtils.i(TAG,"------C----xInit = xInit + dis="+dis);
                }
                out("---ACTION_MOVE 后 --- xInit = "+xInit+"---disdis = "+dis);

                changeValue();
                invalidate();
                break;
            case MotionEvent.ACTION_UP:

                this.getParent().requestDisallowInterceptTouchEvent(false);
                break;
            case MotionEvent.ACTION_CANCEL:

                this.getParent().requestDisallowInterceptTouchEvent(false);
                break;
        }

        return true;
    }


}
